package com.easthome.controller;

import com.easthome.constant.MessageConstant;
import com.easthome.entity.PageResult;
import com.easthome.entity.QueryPageBean;
import com.easthome.pojo.CheckItem;
import com.easthome.result.Result;
import com.easthome.service.CheckItemService;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据表CheckItem的请求和响应
 */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;


    /**
     * 接收检查项数据的修改
     * 参数是检查项对象
     */
    @PutMapping("/editCheckItem")
    public Result editCheckItem(@RequestBody CheckItem checkItem) {
        int row = checkItemService.editCheckItem(checkItem);
        if (row > 0) {
            //修改成功
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    /**
     * 接收检查项数据的主键查询回显
     * 参数为主键
     */
    @GetMapping("/queryCheckItemById")
    public Result queryCheckItemById(Integer id) {
        System.out.println("id = " + id);
        CheckItem checkItem = checkItemService.queryCheckItemById(id);
        if (checkItem != null) {
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
    /**
     * 接收删除检查项数据的请求i
     * 参数是主键id
     */
    @DeleteMapping("/deleteCheckItem")
    public Result deleteCheckItem(Integer id) {
        System.out.println("id = " + id);
        int row = checkItemService.deleteCheckItem(id);
        if (row > 0) {
            //删除成功
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    /**
     * 接收新增检查项请求
     * 参数很多  存储在对象中
     */
    @PostMapping("/addCheckItem")
    public Result addCheckItem(@RequestBody CheckItem checkItem) {
        //调用业务层 传递请求参数 实现数据保存
        int row = checkItemService.addCheckItem(checkItem);
        if (row > 0) {
            //新增成功 响应浏览器
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
    }

    /**
     * 接收查询分页请求
     */
    @PostMapping("/queryCheckItemPage")
    public Result queryCheckItemPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("queryPageBean = " + queryPageBean);
        PageResult pageResult = checkItemService.queryCheckItemPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);


    }

    /**
     * 查询所有的检查项
     */
    @GetMapping("/queryAllCheckItem")
    public Result queryAllCheckItem() {

        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,
                checkItemService.queryAllCheckItem());
    }
}
